package nckd.wnqjt.tmc.tda.plugin.form;

import com.alibaba.fastjson.JSONObject;
import kd.bos.coderule.api.CodeRuleInfo;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.BizDataEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;

import java.util.EventObject;
import java.util.Map;

public class CloseapplybillEditPlugin extends AbstractFormPlugin {

    @Override
    public void createNewData(BizDataEventArgs e) {
        super.createNewData(e);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        Map<String, Object> customParams = this.getView().getFormShowParameter().getCustomParams();
        Object curperiod = customParams.get("curperiod");
        Object org = customParams.get("org");
        Object relateorg = customParams.get("relateorg");
        Object errormsg = customParams.get("errormsg");
        DynamicObject dataEntity = this.getModel().getDataEntity();
        if(org != null){
            dataEntity.set("org", BusinessDataServiceHelper.loadSingle(((JSONObject)org).get("id"), "bos_org"));
        }
        if(relateorg != null){
            dataEntity.set("nckd_org", BusinessDataServiceHelper.loadSingle(((JSONObject)relateorg).get("id"), "bos_org"));
        }
        if(curperiod != null){
            dataEntity.set("nckd_period", BusinessDataServiceHelper.loadSingle(((JSONObject)curperiod).get("id"), "bd_period"));
        }
        dataEntity.set("nckd_checkitem", errormsg);
        CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(dataEntity.getDataEntityType().getName(), dataEntity, null);
        String billno = CodeRuleServiceHelper.getNumber(codeRule, dataEntity);
        dataEntity.set("billno", billno);
    }
}
